package controller;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.User;
import service.UserService;
import utils.LayData;

import java.util.List;

@Controller
@RequestMapping("/asyncuser")

// /user/list
public class AsyncUserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/tolist")
    public String toList() {
        return "asyncuser/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public String findAll(User user, @RequestParam(value = "page",defaultValue = "1") int currentPage,
                          @RequestParam(value = "limit",defaultValue = "10")int pageSize, Model model) {
        // 查询总记录数
        int totalCount = userService.count(user);


        // 计算偏移量
        int offset = (currentPage - 1) * pageSize;
        List<User> users = userService.findAll(user, offset, pageSize);
        LayData laydata = new LayData(0, "查询成功", totalCount, users);
        // 返回json格式数据
        return JSON.toJSONString(laydata);
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "asyncuser/add";
    }

    @RequestMapping("/addSave")

    public String addUser(User user) {
        int result = userService.addUser(user);
        if (result>0) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteUser(@PathVariable("id") int id) {
        int result = userService.deleteUserById(id);
        if (result>0) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateUser(User user) {
        int result = userService.updateUser(user);
        if (result>0) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id,Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "asyncuser/update";
    }

    @RequestMapping("/tologin")
    public String toLogin(){
        return "login";
    }


    @RequestMapping("/login")
    public String login(User user){
        // 登录逻辑
        boolean flag = userService.login(user);
        if(flag){
            // /WEB-INF/login.jsp
            return "index";
        } else {
            return "login";
        }
    }
    @RequestMapping("/toregist")
    public String toRegist(){
        return "regist";
    }
    @RequestMapping("/regist")
    public String regist( User user){
        int result = userService.addUser(user);
        if (result>0) {
            return "success";
        } else {
            return "error";
        }
    }
/*    @RequestMapping("/checkUserNameUnique")
    public String checkUserNameUnique(String userName){
        int result = userService.findUserByName(userName);
        return result > 0 ? "error" : "success";
    }*/

    @RequestMapping("/checkUserNameUnique")
    @ResponseBody
    public String checkUserNameUnique(@RequestParam("name") String name) {
        int result = userService.findUserByName(name);
        return result > 0 ? "error" : "success";
    }





}
